package com.littleHan.brushQuestions.web.userCenter.service.impl;

import com.littleHan.brushQuestions.exception.FailedSettingException;
import com.littleHan.brushQuestions.exception.FailedToInsertException;
import com.littleHan.brushQuestions.exception.FailedToUpdateException;
import com.littleHan.brushQuestions.utils.StringUtil;
import com.littleHan.brushQuestions.web.specificTopic.dao.SpecificTopicDao;
import com.littleHan.brushQuestions.web.start.pojo.Comment;
import com.littleHan.brushQuestions.web.start.pojo.GeneralUser;
import com.littleHan.brushQuestions.web.start.pojo.Topic;
import com.littleHan.brushQuestions.web.userCenter.dao.AuditorUserCenterDao;
import com.littleHan.brushQuestions.web.userCenter.pojo.Notice;
import com.littleHan.brushQuestions.web.userCenter.pojo.NoticeContent;
import com.littleHan.brushQuestions.web.userCenter.pojo.Pagination;
import com.littleHan.brushQuestions.web.userCenter.service.AuditorUserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuditorUserCenterServiceImpl implements AuditorUserCenterService {

    @Autowired
    private AuditorUserCenterDao auditorUserCenterDao;

    @Autowired
    private SpecificTopicDao specificTopicDao;

    @Override
    public Map<String,Object> getNoCheckTopic(Pagination pagination) {
        //获取没有被审核的题目信息（并进行分页）
        List<Topic> topicList=auditorUserCenterDao.getNoCheckTopic(pagination);
        //获取没有被审核题目的数量
        int total=auditorUserCenterDao.getNoCheckTopicNum();
        //获取题目的难度、类型、学科信息
        for(Topic t:topicList){
            String difficult=specificTopicDao.getDiffTypeObj(t.getDifficultyLevelId());
            String type=specificTopicDao.getDiffTypeObj(t.getTopicTypeId());
            String object=specificTopicDao.getDiffTypeObj(t.getObjectId());
            t.setDifficultyLevelId(difficult);
            t.setTopicTypeId(type);
            t.setObjectId(object);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("topicList",topicList);
        map.put("total",total);
        return map;
    }

    @Override
    @Transactional(rollbackFor = {FailedSettingException.class})
    public void topicPassed(Topic topic) throws FailedSettingException{
        //将isCheck改为1
        int result=auditorUserCenterDao.updateIsCheck(topic.getId());
        if(result!=1){
            throw new FailedToUpdateException("审核通过失败");
        }
        //根据用户id获取用户的昵称
        String nickName=auditorUserCenterDao.getGeneralUserNickName(topic.getUserId());
        //通知用户题目通过
        //先再消息内容表中添加内容
        NoticeContent noticeContent=new NoticeContent();
        String noticeContentId=StringUtil.generateUUID();
        noticeContent.setId(noticeContentId);
        noticeContent.setTime(StringUtil.getSysTime());
        noticeContent.setContent("恭喜你，用户："+nickName+",上传的题目通过了审核");
        //插入到消息通知内容表里
        int setNoticeContentResult=auditorUserCenterDao.setNoticeContent(noticeContent);
        if(setNoticeContentResult!=1){
            throw new FailedToInsertException("插入消息通知内容失败");
        }
        //添加消息通知表
        Notice notice=new Notice();
        notice.setId(StringUtil.generateUUID());
        notice.setUserId(topic.getUserId());
        notice.setNoticeContentId(noticeContentId);
        notice.setIsRead(0);
        //插入到消息通知表里
        int setNoticeResult=auditorUserCenterDao.setNotice(notice);
        if(setNoticeResult!=1){
            throw new FailedToInsertException("插入消息通知失败");
        }
    }

    @Override
    @Transactional(rollbackFor = {FailedSettingException.class})
    public void topicUnPassed(Topic topic) throws FailedSettingException{
        //将题目删除
        int deleteResult=auditorUserCenterDao.topicUnPassed(topic.getId());
        if(deleteResult!=1){
            throw new FailedToUpdateException("审核通过失败");
        }
        //根据用户id获取用户的昵称
        String nickName=auditorUserCenterDao.getGeneralUserNickName(topic.getUserId());
        //通知用户题目通未通过
        //先再消息内容表中添加内容
        NoticeContent noticeContent=new NoticeContent();
        String noticeContentId=StringUtil.generateUUID();
        noticeContent.setId(noticeContentId);
        noticeContent.setTime(StringUtil.getSysTime());
        noticeContent.setContent("很遗憾，用户："+nickName+",上传的题目未能通过审核");
        //插入到消息通知内容表里
        int setNoticeContentResult=auditorUserCenterDao.setNoticeContent(noticeContent);
        if(setNoticeContentResult!=1){
            throw new FailedToInsertException("插入消息通知内容失败");
        }
        //添加消息通知表
        Notice notice=new Notice();
        notice.setId(StringUtil.generateUUID());
        notice.setUserId(topic.getUserId());
        notice.setNoticeContentId(noticeContentId);
        notice.setIsRead(0);
        //插入到消息通知表里
        int setNoticeResult=auditorUserCenterDao.setNotice(notice);
        if(setNoticeResult!=1){
            throw new FailedToInsertException("插入消息通知失败");
        }
    }

    @Override
    public Map<String, Object> getNoCheckComment(Pagination pagination) {
        //获取没有被审核的评论信息（并进行分页）
        List<Comment> commentList=auditorUserCenterDao.getNoCheckComment(pagination);
        //获取没有被审核评论的数量
        int total=auditorUserCenterDao.getNoCheckCommentNum();
        Map<String,Object> map=new HashMap<>();
        map.put("commentList",commentList);
        map.put("total",total);
        return map;
    }

    @Override
    @Transactional(rollbackFor = {FailedSettingException.class})
    public void commentPassed(Comment comment) throws FailedSettingException{
        //将isCheck改为1
        int result=auditorUserCenterDao.updateCommentIsCheck(comment.getId());
        if(result!=1){
            throw new FailedToUpdateException("审核通过失败");
        }
    }

    @Override
    @Transactional(rollbackFor = {FailedSettingException.class})
    public void commentUnPassed(Comment comment) throws FailedSettingException{
        //将评论删除（假删）
        int deleteResult=auditorUserCenterDao.commentUnPassed(comment.getId());
        if(deleteResult!=1){
            throw new FailedToUpdateException("评论审核通过失败");
        }
        //根据用户id获取用户的昵称
        String nickName=auditorUserCenterDao.getGeneralUserNickName(comment.getUserId());
        //通知用户题目通未通过
        //先再消息内容表中添加内容
        NoticeContent noticeContent=new NoticeContent();
        String noticeContentId=StringUtil.generateUUID();
        noticeContent.setId(noticeContentId);
        noticeContent.setTime(StringUtil.getSysTime());
        noticeContent.setContent("用户："+nickName+",您的不正当发言已被删除");
        //插入到消息通知内容表里
        int setNoticeContentResult=auditorUserCenterDao.setNoticeContent(noticeContent);
        if(setNoticeContentResult!=1){
            throw new FailedToInsertException("插入消息通知内容失败");
        }
        //添加消息通知表
        Notice notice=new Notice();
        notice.setId(StringUtil.generateUUID());
        notice.setUserId(comment.getUserId());
        notice.setNoticeContentId(noticeContentId);
        notice.setIsRead(0);
        //插入到消息通知表里
        int setNoticeResult=auditorUserCenterDao.setNotice(notice);
        if(setNoticeResult!=1){
            throw new FailedToInsertException("插入消息通知失败");
        }
    }

    @Override
    public GeneralUser getUserInfoByUserId(String userId) {
        return auditorUserCenterDao.getUserInfoByUserId(userId);
    }
}
